#include "ModelPreview.h"
#include "CppHighlighter.h"
#include "../../Tools/Settings.h"
#include "gen2/Formatter.h"
#include "StringTools.h"
#include <sstream>


ModelPreview::ModelPreview(void)
{
	new Highlighter(document());
	setReadOnly(true);
}


ModelPreview::~ModelPreview(void)
{
}

QString ModelPreview::modelToCpp(const SemanticModel::Model& model)
{
	SemanticModel::Analyser analyser(model);
	ObjectModel::CppFormatter cpp(analyser.result());
	cpp.setAuthor(toWString(Settings.getAuthorName()).c_str());
	cpp.setLicense(toWString(Settings.getLicense()).c_str());
	cpp.setCopyright(toWString(Settings.getCopyright()).c_str());
	cpp.build();
	const ObjectModel::CppFormatter::Results& sources = cpp.results();

	std::wstring slashes(cpp.maxLineLength(), L'/');
	const wchar_t* newLine = L"\r\n";

	std::wstringstream sSeparator;
	sSeparator << std::endl << std::endl << slashes << 
		std::endl << slashes << std::endl << std::endl;
	const std::wstring& separator = sSeparator.str();
	
	std::wstringstream sAllSources;
	for(ObjectModel::CppFormatter::Results::const_reverse_iterator file = sources.rbegin(); file != sources.rend(); ++file)
	{
		sAllSources << file->second;
	//	if (file + 1 != sources.rend())
			sAllSources << separator;
	}

	const std::wstring& allSources = sAllSources.str();
	QString result = toQString(allSources);
	return result;
}

bool ModelPreview::dirty()
{
return true;
}

void ModelPreview::setDirty(bool dirty)
{
}

const QString& ModelPreview::fileFilter()
{
	static const QString ext = "cpp";
	return ext;
}

void ModelPreview::clear()
{
}

bool ModelPreview::save(const QString& path)
{
return true;
}

bool ModelPreview::load(const QString& path)
{
return true;
}


SemanticModel::ModelPtr ModelPreview::generateModel()
{
	return SemanticModel::ModelPtr(new SemanticModel::Model());
}

void ModelPreview::setModel(const SemanticModel::Model& m)
{
	QString cpp = modelToCpp(m);
	setPlainText(cpp);
}
